// pages/users/components/score/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    visible: {
      type: Boolean,
      value: false
    },
    initData: {
      type: Object,
      value: {}
    }
  },
  observers: {
    'initData': function (value) {
      if (value) {
        this.setData({
          ...value,
          newScore:'',
          newRanking:''
        })
      }

    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    bottomLift: getApp().globalData.bottomLift
  },

  /**
   * 组件的方法列表
   */
  methods: {
    hide() {
      this.setData({
        visible: false
      })
    },

    inputChange(e) {
      const {
        type
      } = e.currentTarget.dataset
      this.setData({
        [type]: e.detail.value
      })
      if (type == 'newScore' && e.detail.value.length == 3) {
        this.getRank()
      }
    },
    async getRank() {
      const {
        newScore,
        provinceId,
        firstSelect,
      } = this.data
      if (newScore.length < 3) return
      const newRanking = await getApp().getRank(newScore,
        provinceId,
        '2022',
        firstSelect)
      this.setData({
        newRanking
      })
    },
    async confirm() {
      const {
        firstSelect,
        secondSelect,
        newScore,
        newRanking,
        provinceId,
        province,
      } = this.data
      if(newScore==''||newRanking=='')return
      this.hide()
      this.triggerEvent('confirm', {
        firstSelect,
        secondSelect,
        score:newScore,
        ranking:newRanking,
        provinceId,
        province
      })
    }
  }
})